<template>
  <confirm-bar :visible="rowSelection">
    <template #content>
      已选&nbsp;{{ selectedRowKeys.length }}&nbsp;项
    </template>
    <template #handle>
      <a-button type="link" @click="onCancel">取消选择</a-button>
      <a-button
        type="link"
        @click="onConfirm"
        :loading="loading"
        :disabled="selectedRowKeys.length === 0"
      >
        发布
      </a-button>
    </template>
  </confirm-bar>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import ConfirmBar from '@/components/confirm-bar/confirm-bar.vue'
import { useRowSelectionActive, useRowSelection } from './provide-context.js'
import { publishUser } from '@/api/user.js'
import { useDataSourceManager } from './provide-context.js'
/**
 * 全局
 */
let { update } = useDataSourceManager()
let rowSelection = useRowSelectionActive()
let { selectedRowKeys } = useRowSelection()
/**
 * 操作逻辑
 */
let loading = ref(false)
function onCancel() {
  selectedRowKeys.value = []
  rowSelection.value = false
}
async function onConfirm() {
  loading.value = true
  try {
    let params = {
      userIds: selectedRowKeys.value,
    }
    await publishUser(params)
    await update()
    message.success('发布成功')
  } catch (e) {
    message.error('发布失败')
  }
  rowSelection.value = false
  selectedRowKeys.value = []
  loading.value = false
}
</script>
